﻿//========================================================= 
/**@file ComCmdDriver.h
 * @brief 串口设备命令
 * 
 * @date 2016-09-28   23:00:50
 * @author Zhyioo 
 * @version 1.0
 */ 
//--------------------------------------------------------- 
#ifndef _LIBZHOUYB_COMCMDDRIVER_H_
#define _LIBZHOUYB_COMCMDDRIVER_H_
//--------------------------------------------------------- 
#include "../CommonCmdDriver.h"

#ifdef _WIN32
#   include "../../../base_device/win32/ComDevice.h"
#else
#   include "../../../base_device/linux/ComDevice.h"
#endif

#include "../../tools/ParamHelper.h"
using zhou_yb::application::tools::ParamHelper;
//--------------------------------------------------------- 
namespace zhou_yb {
namespace application {
namespace driver {
//--------------------------------------------------------- 
/// 串口设备命令驱动
class ComCmdDriver : public CommonCmdDriver
{
protected:
    Ref<IBaseDevice> _activeDev;
    Ref<IInteractiveTrans> _activeTrans;
    Ref<ITimeoutBehavior> _activeTimeout;
    ComDevice _comdev;
public:
    ComCmdDriver() : CommonCmdDriver()
    {
        _activeTrans = _comdev;
        _activeDev = _comdev;
        _activeTimeout = _comdev;

        _errGetter.Add(_comdev);
        _logSetter.select(_comdev);
        
        _RegisteCMD(ComCmdDriver, EnumDevice);

        _RegisteIBaseDeviceCMD(ComCmdDriver);
        _RegisteIInteractiveTransCMD(ComCmdDriver);
        _RegisteITimeoutCMD(ComCmdDriver);
        _RegisteCancelCMD(ComCmdDriver);

        _RegisteInterfaceCMD(ComCmdDriver, IBaseDevice);
        _RegisteInterfaceCMD(ComCmdDriver, IInteractiveTrans);
        _RegisteInterfaceCMD(ComCmdDriver, ITimeoutBehavior);
    }
    /* Interface */
    LC_CMD_INTERFACE_GETTER(IBaseDevice, _activeDev);
    LC_CMD_INTERFACE_GETTER(IInteractiveTrans, _activeTrans);
    LC_CMD_INTERFACE_GETTER(ITimeoutBehavior, _activeTimeout);
    /**
     * @brief 枚举本地串口设备
     * @date 2017-02-27 20:12
     * 
     * @retval Port : string 串口参数(COM)
     * @retval Count : uint 枚举到的串口数
     */
    LC_CMD_METHOD(EnumDevice)
    {
        list_t<uint> comlist;
        _comdev.EnumDevice(comlist);

        list_t<uint>::iterator itr;
        ByteBuilder comName(8);
        for(itr = comlist.begin();itr != comlist.end(); ++itr)
        {
            comName.Clear();
            comName.Format("COM%d", *itr);

            rlt.Put("Port", comName.GetString());
        }
        rlt.Put("Count", ArgConvert::ToString(comlist.size()));
        return true;
    }
    /**
     * @brief 打开串口设备
     * @date 2016-09-28 23:02
     * 
     * @param [in] Port : string [default:Auto] 需要打开的串口参数
     * @param [in] Baud : uint [default:9600] 打开串口的波特率
     */
    LC_CMD_METHOD(Open)
    {
        string sPort = arg["Port"].To<string>("AUTO");
        uint port = ParamHelper::ParseCOM(sPort.c_str());
        uint baud = arg["Baud"].To<uint>(9600);

        if(ComDeviceHelper::OpenDevice<ComDevice>(_comdev, port, baud) != DevHelper::EnumSUCCESS)
            return false;
        return true;
    }
    /// 返回设备是否打开
    LC_CMD_METHOD(IsOpen)
    {
        ASSERT_FuncErr(_comdev.IsOpen(), DeviceError::DevAlreadyCloseErr);
        return true;
    }
    /// 关闭设备
    LC_CMD_METHOD(Close)
    {
        _comdev.Close();
        return true;
    }

    LC_CMD_TIMEOUT(_comdev);
    LC_CMD_CANCEL(_comdev);
    LC_CMD_INTERACTIVETRANS(_comdev);
};
//--------------------------------------------------------- 
} // nemespace driver
} // namespace application
} // namespace zhou_yb
//--------------------------------------------------------- 
#endif // _LIBZHOUYB_COMCMDDRIVER_H_
//========================================================= 